package com.cmict.data.bpm.service.definition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.bpm.controller.admin.definition.vo.task.TaskVariableCreateReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.task.TaskVariableExportReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.task.TaskVariablePageReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.task.TaskVariableUpdateReqVO;
import com.cmict.data.bpm.controller.admin.task.vo.task.CarbonTaskInfoVo;
import com.cmict.data.bpm.convert.definition.TaskVariableConvert;
import com.cmict.data.bpm.dal.dataobject.definition.TaskVariableDO;
import com.cmict.data.bpm.dal.mysql.definition.TaskVariableMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cmict.data.bpm.enums.ErrorCodeConstants.TASK_VARIABLE_NOT_EXISTS;

/**
 * 流程变量 Service 实现类
 *
 * @author liutao
 * @date 2022/6/6
 */
@Service
@Validated
public class TaskVariableServiceImpl implements TaskVariableService {

    @Resource
    private TaskVariableMapper taskVariableMapper;

    @Override
    public Long createTaskVariable(TaskVariableCreateReqVO createReqVO) {
        // 插入
        TaskVariableDO taskVariable = TaskVariableConvert.INSTANCE.convert(createReqVO);
        taskVariableMapper.insert(taskVariable);
        // 返回
        return taskVariable.getId();
    }

    @Override
    public void updateTaskVariable(TaskVariableUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTaskVariableExists(updateReqVO.getId());
        // 更新
        TaskVariableDO updateObj = TaskVariableConvert.INSTANCE.convert(updateReqVO);
        taskVariableMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskVariable(Long id) {
        // 校验存在
        this.validateTaskVariableExists(id);
        // 删除
        taskVariableMapper.deleteById(id);
    }

    private void validateTaskVariableExists(Long id) {
        if (taskVariableMapper.selectById(id) == null) {
            throw exception(TASK_VARIABLE_NOT_EXISTS);
        }
    }

    @Override
    public TaskVariableDO getTaskVariable(Long id) {
        return taskVariableMapper.selectById(id);
    }

    @Override
    public List<TaskVariableDO> getTaskVariableList(Collection<Long> ids) {
        return taskVariableMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TaskVariableDO> getTaskVariablePage(TaskVariablePageReqVO pageReqVO) {
        return taskVariableMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TaskVariableDO> getTaskVariableList(TaskVariableExportReqVO exportReqVO) {
        return taskVariableMapper.selectList(exportReqVO);

    }
    @Override
    public TaskVariableDO selectOne(CarbonTaskInfoVo taskInfoVo) {
        QueryWrapper<TaskVariableDO> qw = new QueryWrapper<>();
        qw.lambda().eq(TaskVariableDO::getTaskId, taskInfoVo.getTaskId())
                .eq(TaskVariableDO::getProcessInstanceId, taskInfoVo.getProcessInstanceId())
                .orderByDesc(TaskVariableDO::getId);
        List<TaskVariableDO> taskVariables = taskVariableMapper.selectList(qw);
        if(CollectionUtils.isEmpty(taskVariables)){
            return null;
        }
        return taskVariables.get(0);
    }

    @Override
    public void deleteTaskVariableWithTaskId(String taskId) {
        // 删除
        if (StringUtils.isNotBlank(taskId)) {
            QueryWrapper<TaskVariableDO> qw = new QueryWrapper<>();
            qw.lambda().eq(TaskVariableDO::getTaskId, taskId);
            taskVariableMapper.delete(qw);
        }
    }
}
